﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Newtonsoft.Json;
using System.Text;
using CRMWeb.Utilities.Context;
using CRMFramework;
using CRMEntity;
using CRMBiz;


namespace CRMWeb.WebServices
{
    /// <summary>
    /// Summary description for Test1Handler
    /// </summary>
    public class ApplicationSelect : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                var pageIndex = string.IsNullOrEmpty(context.Request["page"]) ? 1 : int.Parse(context.Request["page"]);
                var pageSize = string.IsNullOrEmpty(context.Request["rows"]) ? 10 : int.Parse(context.Request["rows"]);
                var searchtext = string.IsNullOrEmpty(context.Request.QueryString["searchtext"]) ? "" : context.Request.QueryString["searchtext"];
                var conditiontext = string.IsNullOrEmpty(context.Request.QueryString["conditiontext"]) ? "" : context.Request.QueryString["conditiontext"];
                var sortText = string.IsNullOrEmpty(context.Request.QueryString["sortCondition"]) ? "" : context.Request.QueryString["sortCondition"];

                if (conditiontext.Contains(';'))
                {
                    conditiontext = conditiontext.Replace(';', '%');
                }
                var days = string.IsNullOrEmpty(context.Request.QueryString["days"]) ? "All" : context.Request.QueryString["days"];
                Int32 totalAmount = 0;               
                DataSet ds = ApplicationListHandler.GetApplicationList(pageIndex, pageSize, searchtext, conditiontext, sortText, out totalAmount, days);
                var strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                strJson = "{\"total\":\"" + totalAmount.ToString() + "\",\"rows\":" + strJson + "}";
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}